package com.wr.activeMQ;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.jms.Destination;
import java.util.HashMap;
import java.util.Map;

@Service
public class Producer {
    @Autowired // 也可以注入JmsTemplate，JmsMessagingTemplate对JmsTemplate进行了封装

    private JmsMessagingTemplate jmsTemplate;

    // 发送消息，destination是发送到的队列，message是待发送的消息
    public void sendMessage(Destination destination, final Map<String,MultipartFile> message){
        for (Map.Entry<String, MultipartFile> entity : message.entrySet()){
            MultipartFile file = entity.getValue();
            try {
                byte[] data = file.getBytes();
                jmsTemplate.convertAndSend(destination, data);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
